package z_exam;

import java.util.*;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/21 19:00
 */
public class Test4 {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //目标服务号
        int m = sc.nextInt();
        // 建立图和入度表
        HashMap<Integer, LinkedList<Integer>> graph = new HashMap<>();
        for(int i = 0; i < n; i++) {
            graph.put(i, new LinkedList<>());
        }
        int[] inDegree = new int[n];
        for (int i = 0; i < n; i++) {
            String next = sc.next();
            for (int j = 1; j < next.length(); j++) {
                if (next.charAt(j) != ',') {
                    // prerequisites[i][1] -> prerequisites[i][0]
                    graph.get(next.charAt(j) - '0').add(i);
                    //入度++
                    inDegree[i]++;
                }
            }
        }
        System.out.println(findOrder(graph, inDegree, n, m));
    }


    public static String findOrder (HashMap<Integer, LinkedList<Integer>> graph, int[] inDegree, int n, int m) {
        Queue<Integer> queue = new LinkedList<>();
        LinkedList<Integer> list = new LinkedList<>();
        // 选择入度为0的作为起点
        for(int course = 0; course < n; course++){
            if(inDegree[course] == 0){
                if (course == m) {
                    return "null";
                }
                list.add(course);
                queue.offer(course);
            }
        }
        // 没找到入度为0的点，存在环
        if(queue.isEmpty()) {
            return "-1";
        }
        // 拓扑排序
        while(!queue.isEmpty()){
            int course = queue.poll();
            for(int neighbor: graph.get(course)){
                inDegree[neighbor] --;
                if(inDegree[neighbor] == 0){
                    // 每次将入度为0的点作为下一个起点
                    list.add(neighbor);
                    queue.offer(neighbor);
                }
            }
        }
        Iterator<Integer> iterator = list.iterator();
        StringBuilder result = new StringBuilder();
        for(int i = 0; i < m; i++){
            result.append(iterator.next());
            if (i != m - 1) {
                result.append(",");
            }
        }
        return result.toString();
    }
}
